/**
 * 
 */
package com.vector.ding.context;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;

import com.vector.ding.exception.BaseException;
import com.vector.ding.safe.IUser;

/**
 * @author vector
 *
 */
public final class AuthContext {
	
	private AuthContext() {
		
	}
	
	/**
	 * get shiro subject 
	 * @return
	 */
	public static Subject getCurrentSubject() {
		return SecurityUtils.getSubject();
	}
	
	/**
	 * current user isAuthenticated
	 * @return
	 */
	public static boolean isAuthenticated() {
		return getCurrentSubject().isAuthenticated();
	}
	
	/**
	 * get current user
	 * @return
	 */
	public static IUser getCurrentUser() {
		Subject subject = SecurityUtils.getSubject();
		if (subject == null || !subject.isAuthenticated())
			throw new BaseException("获取当前用户失败！");
		
		return (IUser) subject.getPrincipal();
	}
	
	/**
	 * current user is admin or not
	 * @return
	 */
	public static boolean isAdmin() {
		IUser curUser = getCurrentUser();
		return curUser.getLoginName().equals("admin");
				
	}
}
